package com.lujianfei.babymilkmemory.utils


/**
 * Author: lujianfei
 * Date: 2025/2/18 16:22
 * Description:
 */


import android.util.Log
import java.util.Locale


object MyLogger {

    fun d(tag:String, msg:String) {
        print(tag, msg, LogLevel.Debug)
    }

    fun i(tag:String, msg:String) {
        print(tag, msg, LogLevel.Info)
    }

    fun e(tag:String, msg:String) {
        print(tag, msg, LogLevel.Error)
    }

    fun w(tag:String, msg:String) {
        print(tag, msg, LogLevel.Warning)
    }

    private fun print(tag:String, msg:String, level: LogLevel) {
        val index = 4
        val stringBuffer = StringBuffer()
        //打印栈帧中的所有方法调用
        try {
            val stackTrace = Thread.currentThread().stackTrace
            val className = stackTrace[index].fileName
            var methodName = stackTrace[index].methodName
            val lineNumber = stackTrace[index].lineNumber
            methodName =
                methodName.substring(0, 1).uppercase(Locale.getDefault()) + methodName.substring(1)
            stringBuffer.append("[(").append(className).append(":").append(lineNumber).append(")#")
                .append(methodName).append("] ")
        } catch (e: Exception) {
            e.printStackTrace()
        }

        val finalMsg = "$stringBuffer: $msg"
        when (level) {
            LogLevel.Info-> {
                Log.i(tag, finalMsg)
            }
            LogLevel.Warning-> {
                Log.w(tag, finalMsg)
            }
            LogLevel.Debug-> {
                Log.d(tag, finalMsg)
            }
            LogLevel.Error -> {
                Log.e(tag, finalMsg)
            }
        }
    }

    fun printAllTrace(tag:String, level: LogLevel = LogLevel.Debug) {
        val stringBuffer = StringBuffer()
        //打印栈帧中的所有方法调用
        try {
            val stackTrace = Thread.currentThread().stackTrace
            for (index in stackTrace.indices) {
                val className = stackTrace[index].fileName
                var methodName = stackTrace[index].methodName
                val lineNumber = stackTrace[index].lineNumber
                methodName =
                    methodName.substring(0, 1).uppercase(Locale.getDefault()) + methodName.substring(1)
                stringBuffer.append("[(").append(className).append(":").append(lineNumber).append(")#")
                    .append(methodName).append("] ").append("\r\n")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        val finalMsg = "$stringBuffer"
        when (level) {
            LogLevel.Info-> {
                Log.i(tag, finalMsg)
            }
            LogLevel.Warning-> {
                Log.w(tag, finalMsg)
            }
            LogLevel.Debug-> {
                Log.d(tag, finalMsg)
            }
            LogLevel.Error -> {
                Log.e(tag, finalMsg)
            }
        }
    }
}

enum class LogLevel {
    Debug,
    Info,
    Error,
    Warning
}
